package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.model.ShopDatabase;
import com.ruoyi.system.domain.BaseShopDatabase;
import com.ruoyi.system.service.IBaseShopDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2022-10-20
 */
@RestController
@RequestMapping("/shopdatabase")
public class BaseShopDatabaseController extends BaseController
{
    @Autowired
    private IBaseShopDatabaseService baseShopDatabaseService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:database:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseShopDatabase baseShopDatabase)
    {
        startPage();
        List<BaseShopDatabase> list = baseShopDatabaseService.selectBaseShopDatabaseList(baseShopDatabase);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:database:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseShopDatabase baseShopDatabase)
    {
        List<BaseShopDatabase> list = baseShopDatabaseService.selectBaseShopDatabaseList(baseShopDatabase);
        ExcelUtil<BaseShopDatabase> util = new ExcelUtil<BaseShopDatabase>(BaseShopDatabase.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("system:database:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseShopDatabaseService.selectBaseShopDatabaseById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("system:database:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseShopDatabase baseShopDatabase)
    {
        return toAjax(baseShopDatabaseService.insertBaseShopDatabase(baseShopDatabase));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:database:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseShopDatabase baseShopDatabase)
    {
        return toAjax(baseShopDatabaseService.updateBaseShopDatabase(baseShopDatabase));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:database:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseShopDatabaseService.deleteBaseShopDatabaseByIds(ids));
    }

    /**
     * 获取当前用户信息
     */
    @InnerAuth
    @GetMapping("/shopdatabaseinfo/{shopcode}/{databasename}")
    public R<ShopDatabase> userinfo(@PathVariable("shopcode") String shopcode, @PathVariable("databasename") String databasename) {

        if (StringUtils.isBlank(shopcode))
        {
            return R.fail("没有店铺信息。");
        }
        if (StringUtils.isBlank(databasename))
        {
            return R.fail("没有数据库信息。");
        }
        BaseShopDatabase baseshopdatabase = new BaseShopDatabase();
        baseshopdatabase.setShopCode(shopcode);
        baseshopdatabase.setDatabaseName(databasename);
        List<BaseShopDatabase> shopDatabases = baseShopDatabaseService.selectBaseShopDatabaseList(baseshopdatabase);
        if(shopDatabases.isEmpty())
        {
            return R.fail("没有获得数据库信息。");
        }
        ShopDatabase shopDatabase = new ShopDatabase();
        shopDatabase.setDatabaseCode(shopDatabases.get(0).getDatabaseCode());
        shopDatabase.setDatabaseName(shopDatabases.get(0).getDatabaseName());
        shopDatabase.setShopCode(shopDatabases.get(0).getShopCode());
        return R.ok(shopDatabase);
    }

}
